/* Copyright 2007-2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Patrick van der Velde
 * Marcus Cuda
 * Jurgen Van Gael
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */
using System;
using System.IO;

namespace dnAnalytics.LinearAlgebra.IO
{
    /// <summary>
    /// Base class to write a single <see cref="Matrix"/> to a file or stream.
    /// </summary>
    public abstract class SingleMatrixWriter : ISingleMatrixWriter
    {
        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given file. If the file already exists, 
        /// the file will be overwritten.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="file">The file to write the matrix to.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, string file)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            using (StreamWriter writer = new StreamWriter(file))
            {
                DoWriteMatrix(matrix, writer, null);
            }
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given file. If the file already exists, 
        /// the file will be overwritten.
        /// </summary>
        /// <param name="matrix">the matrix to write.</param>
        /// <param name="file">The file to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, string file, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (File.Exists(file))
            {
                File.Delete(file);
            }

            using (StreamWriter writer = new StreamWriter(file) )
            {
                DoWriteMatrix(matrix, writer, format);
            }
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given stream.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, Stream stream)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (StreamWriter writer = new StreamWriter(new BufferedStream(stream)))
            {
                DoWriteMatrix(matrix, writer, null);
            }
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given stream.
        /// </summary>
        /// <param name="matrix">The <see cref="TextWriter"/> to write.</param>
        /// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, Stream stream, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (StreamWriter writer = new StreamWriter(new BufferedStream(stream)))
            {
                DoWriteMatrix(matrix, writer, format);
            }
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given <b>TextWriter</b>.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, TextWriter writer)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            DoWriteMatrix(matrix, writer, null);
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given <b>TextWriter</b>.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, TextWriter writer, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            DoWriteMatrix(matrix, writer, format);
        }

        /// <summary>
        /// Subclasses must implement this method to do the actually writing.
        /// </summary>
        /// <param name="matrix">The matrix to serialize.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <param name="format">The format for the new matrix.</param>
        protected abstract void DoWriteMatrix(Matrix matrix, TextWriter writer, string format);
    }
}
